import Vue from "vue";
import VueRouter from "vue-router";
import layout from "../pages/layout/layout.vue";
// import ErrorPage from "../pages/error/404.vue";
import { getToken } from "@/assets/overall/js/module/token";
import config from "@/config";
import { get as GetSessionMenusData } from "@/assets/overall/js/module/sessionStorage";
import formatModule from "@/assets/overall/js/module/format";
// import {mapGetters} from 'vuex'
/**
 *  @name 解决Vue-router 函数式编程重复点击导航控制台报错
 *  @url https://blog.csdn.net/luer_LJS/article/details/108362563?spm=1001.2101.3001.6650.13&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-13-108362563-blog-123013967.pc_relevant_3mothn_strategy_recovery&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-13-108362563-blog-123013967.pc_relevant_3mothn_strategy_recovery&utm_relevant_index=14
 *
 **/
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
Vue.use(VueRouter);
const router = new VueRouter({
  mode: "hash",
  routes: [
    {
      name: "layout",
      path: "/",
      component: layout,
      children: [],
    },
    {
      name: "login",
      path: "/login",
      component: () => import("../pages/login"),
    },
    {
      name: "403",
      path: "/403",
      component: () => import("../pages/error/403"),
    },
    {
      path: "*",
      component: () => import("../pages/error/404"),
    },
  ],
});
// 路由全局前置守卫
router.beforeEach(function (to, form, next) {
  // 是否开启Token验证
  let ifTokenVerify = config.tokenVerify;
  if (ifTokenVerify) {
    // 登录页面路由地址
    let loginPath = "/login";
    let tokenValue = getToken();
    //   如果访问的是登录页面，直接放行
    if (to.path == loginPath) {
      next();
    } else {
      // 如果有Token直接放行
      if (tokenValue) {
        let vuex = router.app.$store.state.overall;
        let getVueRouterList = vuex.navigatorMenuList;
        if (!getVueRouterList.length) {
          // 获取本地存储的路由信息
          let getLocalStorageMenusList = JSON.parse(
            GetSessionMenusData("TardeMenusData")
          );
          if (getLocalStorageMenusList) {
            // 将路由信息格式化
            let ArrayFormatVueRouter = formatModule.ArrayFormatVueRouter(
              getLocalStorageMenusList
            );
            // 将格式化后的数组动态添加到VueRouter
            ArrayFormatVueRouter.forEach((item) => {
              router.addRoute("layout", item);
            });
            // 动态添加到VueX
            router.app.$store.commit(
              "setNavigatorMenuOriginData",
              getLocalStorageMenusList
            );
            next(to.path);
          } else {
            next(loginPath);
          }
        }
        next();
      } else {
        // 如果没有token，且当前是登录页，无需操作
        if (form.fullPath == loginPath) {
          console.log({
            code: 0,
            msg: "登录失败,路由拦截无token",
          });
        } else {
          next(loginPath);
        }
      }
    }
  } else {
    next();
  }
});
export default router;
